package info.xiaomo.gengine.ai.nav.polygon.ui;

import info.xiaomo.gengine.math.Vector3;
import info.xiaomo.gengine.utils.TimeUtil;

/** navmesh主界面 */
public class PolygonNavStart extends javax.swing.JFrame {

    private PolygonMeshWindow mapWindow;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton locationButton;

    //    static TriangleBlock addPolygons = null;
    private javax.swing.JTextField locationX;
    private javax.swing.JTextField locationY;
    private javax.swing.JTextArea logTextArea;
    private javax.swing.JTextField position;
    private javax.swing.JButton randomPointButton;
    private javax.swing.JComboBox<String> scaleComboBox;
    private javax.swing.JButton showRandomPointButton;
    private javax.swing.JButton triangleIndexButton;
    private javax.swing.JButton vectorIndexButton;

    /** Creates new form Main */
    public PolygonNavStart() {
        initComponents();
    }

    /** @param args the command line arguments */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        // <editor-fold desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info :
                    javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PolygonNavStart.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PolygonNavStart.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PolygonNavStart.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PolygonNavStart.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        // </editor-fold>
        // </editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(
                new Runnable() {
                    public void run() {
                        PolygonNavStart main = new PolygonNavStart();
                        main.setVisible(true);
                        PolygonMeshWindow map = new PolygonMeshWindow(main);
                    }
                });
    }

    /** @param mapWindow the mapWindow to set */
    public void setMapWindow(PolygonMeshWindow mapWindow) {
        this.mapWindow = mapWindow;
    }

    public void setPosition(Vector3 pos) {
        position.setText(pos.toString());
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        position = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        randomPointButton = new javax.swing.JButton();
        showRandomPointButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        logTextArea = new javax.swing.JTextArea();
        triangleIndexButton = new javax.swing.JButton();
        vectorIndexButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        scaleComboBox = new javax.swing.JComboBox<>();
        jLabel3 = new javax.swing.JLabel();
        locationX = new javax.swing.JTextField();
        locationY = new javax.swing.JTextField();
        locationButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("当前坐标：");

        randomPointButton.setText("随机坐标");
        randomPointButton.addActionListener(
                new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        randomPointButtonActionPerformed(evt);
                    }
                });

        showRandomPointButton.setText("显示随机点");
        showRandomPointButton.addActionListener(
                new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        showRandomPointButtonActionPerformed(evt);
                    }
                });

        logTextArea.setColumns(20);
        logTextArea.setRows(5);
        logTextArea.setText("R键：重置坐标\nP键：暂停\n双击选中多边形\n\n");
        jScrollPane1.setViewportView(logTextArea);

        triangleIndexButton.setText("多边形序号");
        triangleIndexButton.addActionListener(
                new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        triangleIndexButtonActionPerformed(evt);
                    }
                });

        vectorIndexButton.setText("坐标序号");
        vectorIndexButton.addActionListener(
                new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        vectorIndexButtonActionPerformed(evt);
                    }
                });

        jLabel2.setText("缩放倍数：");

        scaleComboBox.setModel(
                new javax.swing.DefaultComboBoxModel<>(
                        new String[] {
                            "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "15", "20", "25",
                            "30", "50"
                        }));
        scaleComboBox.addItemListener(
                new java.awt.event.ItemListener() {
                    public void itemStateChanged(java.awt.event.ItemEvent evt) {
                        scaleComboBoxItemStateChanged(evt);
                    }
                });

        jLabel3.setText("定位坐标：");

        locationX.setText("100");
        locationX.addActionListener(
                new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        locationXActionPerformed(evt);
                    }
                });

        locationY.setText("100");

        locationButton.setText("定位");
        locationButton.addActionListener(
                new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        locationButtonActionPerformed(evt);
                    }
                });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(
                                                layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment
                                                                        .LEADING)
                                                        .addComponent(jScrollPane1)
                                                        .addGroup(
                                                                layout.createSequentialGroup()
                                                                        .addGroup(
                                                                                layout.createParallelGroup(
                                                                                                javax
                                                                                                        .swing
                                                                                                        .GroupLayout
                                                                                                        .Alignment
                                                                                                        .LEADING)
                                                                                        .addGroup(
                                                                                                layout.createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel2)
                                                                                                        .addPreferredGap(
                                                                                                                javax
                                                                                                                        .swing
                                                                                                                        .LayoutStyle
                                                                                                                        .ComponentPlacement
                                                                                                                        .RELATED)
                                                                                                        .addComponent(
                                                                                                                scaleComboBox,
                                                                                                                javax
                                                                                                                        .swing
                                                                                                                        .GroupLayout
                                                                                                                        .PREFERRED_SIZE,
                                                                                                                87,
                                                                                                                javax
                                                                                                                        .swing
                                                                                                                        .GroupLayout
                                                                                                                        .PREFERRED_SIZE))
                                                                                        .addGroup(
                                                                                                layout.createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                randomPointButton)
                                                                                                        .addPreferredGap(
                                                                                                                javax
                                                                                                                        .swing
                                                                                                                        .LayoutStyle
                                                                                                                        .ComponentPlacement
                                                                                                                        .UNRELATED)
                                                                                                        .addComponent(
                                                                                                                showRandomPointButton)
                                                                                                        .addPreferredGap(
                                                                                                                javax
                                                                                                                        .swing
                                                                                                                        .LayoutStyle
                                                                                                                        .ComponentPlacement
                                                                                                                        .RELATED)
                                                                                                        .addComponent(
                                                                                                                triangleIndexButton)
                                                                                                        .addPreferredGap(
                                                                                                                javax
                                                                                                                        .swing
                                                                                                                        .LayoutStyle
                                                                                                                        .ComponentPlacement
                                                                                                                        .RELATED)
                                                                                                        .addComponent(
                                                                                                                vectorIndexButton))
                                                                                        .addGroup(
                                                                                                layout.createSequentialGroup()
                                                                                                        .addGroup(
                                                                                                                layout.createParallelGroup(
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .Alignment
                                                                                                                                        .LEADING,
                                                                                                                                false)
                                                                                                                        .addComponent(
                                                                                                                                jLabel1,
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .DEFAULT_SIZE,
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .DEFAULT_SIZE,
                                                                                                                                Short
                                                                                                                                        .MAX_VALUE)
                                                                                                                        .addComponent(
                                                                                                                                jLabel3,
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .DEFAULT_SIZE,
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .DEFAULT_SIZE,
                                                                                                                                Short
                                                                                                                                        .MAX_VALUE))
                                                                                                        .addPreferredGap(
                                                                                                                javax
                                                                                                                        .swing
                                                                                                                        .LayoutStyle
                                                                                                                        .ComponentPlacement
                                                                                                                        .RELATED)
                                                                                                        .addGroup(
                                                                                                                layout.createParallelGroup(
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .Alignment
                                                                                                                                        .LEADING,
                                                                                                                                false)
                                                                                                                        .addComponent(
                                                                                                                                position,
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .PREFERRED_SIZE,
                                                                                                                                315,
                                                                                                                                javax
                                                                                                                                        .swing
                                                                                                                                        .GroupLayout
                                                                                                                                        .PREFERRED_SIZE)
                                                                                                                        .addGroup(
                                                                                                                                layout.createSequentialGroup()
                                                                                                                                        .addComponent(
                                                                                                                                                locationX,
                                                                                                                                                javax
                                                                                                                                                        .swing
                                                                                                                                                        .GroupLayout
                                                                                                                                                        .PREFERRED_SIZE,
                                                                                                                                                90,
                                                                                                                                                javax
                                                                                                                                                        .swing
                                                                                                                                                        .GroupLayout
                                                                                                                                                        .PREFERRED_SIZE)
                                                                                                                                        .addPreferredGap(
                                                                                                                                                javax
                                                                                                                                                        .swing
                                                                                                                                                        .LayoutStyle
                                                                                                                                                        .ComponentPlacement
                                                                                                                                                        .UNRELATED)
                                                                                                                                        .addComponent(
                                                                                                                                                locationY,
                                                                                                                                                javax
                                                                                                                                                        .swing
                                                                                                                                                        .GroupLayout
                                                                                                                                                        .PREFERRED_SIZE,
                                                                                                                                                85,
                                                                                                                                                javax
                                                                                                                                                        .swing
                                                                                                                                                        .GroupLayout
                                                                                                                                                        .PREFERRED_SIZE)
                                                                                                                                        .addPreferredGap(
                                                                                                                                                javax
                                                                                                                                                        .swing
                                                                                                                                                        .LayoutStyle
                                                                                                                                                        .ComponentPlacement
                                                                                                                                                        .RELATED,
                                                                                                                                                javax
                                                                                                                                                        .swing
                                                                                                                                                        .GroupLayout
                                                                                                                                                        .DEFAULT_SIZE,
                                                                                                                                                Short
                                                                                                                                                        .MAX_VALUE)
                                                                                                                                        .addComponent(
                                                                                                                                                locationButton)))))
                                                                        .addContainerGap(
                                                                                33,
                                                                                Short
                                                                                        .MAX_VALUE)))));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(
                                                layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment
                                                                        .BASELINE)
                                                        .addComponent(randomPointButton)
                                                        .addComponent(showRandomPointButton)
                                                        .addComponent(triangleIndexButton)
                                                        .addComponent(vectorIndexButton))
                                        .addGap(21, 21, 21)
                                        .addGroup(
                                                layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment
                                                                        .BASELINE)
                                                        .addComponent(jLabel2)
                                                        .addComponent(
                                                                scaleComboBox,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .DEFAULT_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment
                                                                        .BASELINE)
                                                        .addComponent(jLabel1)
                                                        .addComponent(
                                                                position,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .DEFAULT_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment
                                                                        .BASELINE)
                                                        .addComponent(
                                                                jLabel3,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE,
                                                                15,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE)
                                                        .addComponent(
                                                                locationX,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .DEFAULT_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE)
                                                        .addComponent(
                                                                locationY,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .DEFAULT_SIZE,
                                                                javax.swing.GroupLayout
                                                                        .PREFERRED_SIZE)
                                                        .addComponent(locationButton))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement
                                                        .UNRELATED)
                                        .addComponent(
                                                jScrollPane1,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                126,
                                                Short.MAX_VALUE)
                                        .addContainerGap()));

        pack();
    } // </editor-fold>//GEN-END:initComponents

    private void randomPointButtonActionPerformed(
            java.awt.event.ActionEvent evt) { // GEN-FIRST:event_randomPointButtonActionPerformed
        mapWindow.randMove();
    } // GEN-LAST:event_randomPointButtonActionPerformed

    private void showRandomPointButtonActionPerformed(
            java.awt.event.ActionEvent
                    evt) { // GEN-FIRST:event_showRandomPointButtonActionPerformed
        mapWindow.polygonViewPane.changeShowRandomPoint();
    } // GEN-LAST:event_showRandomPointButtonActionPerformed

    private void triangleIndexButtonActionPerformed(
            java.awt.event.ActionEvent evt) { // GEN-FIRST:event_triangleIndexButtonActionPerformed
        // 显示隐藏三角形序号
        this.mapWindow.polygonViewPane.changeShowTriangleIndex();
    } // GEN-LAST:event_triangleIndexButtonActionPerformed

    private void vectorIndexButtonActionPerformed(
            java.awt.event.ActionEvent evt) { // GEN-FIRST:event_vectorIndexButtonActionPerformed
        this.mapWindow.polygonViewPane.changeShowVectorIndex();
    } // GEN-LAST:event_vectorIndexButtonActionPerformed

    private void scaleComboBoxItemStateChanged(
            java.awt.event.ItemEvent evt) { // GEN-FIRST:event_scaleComboBoxItemStateChanged
        if (this.mapWindow == null) {
            return;
        }
        this.mapWindow.loadMap(
                PolygonMeshWindow.lastFilePath,
                Integer.parseInt(scaleComboBox.getSelectedItem().toString()));
    } // GEN-LAST:event_scaleComboBoxItemStateChanged

    private void locationXActionPerformed(
            java.awt.event.ActionEvent evt) { // GEN-FIRST:event_locationXActionPerformed
        // TODO add your handling code here:
    } // GEN-LAST:event_locationXActionPerformed

    private void locationButtonActionPerformed(
            java.awt.event.ActionEvent evt) { // GEN-FIRST:event_locationButtonActionPerformed
        try {
            this.mapWindow.setLocationVector(
                    Float.parseFloat(locationX.getText()), Float.parseFloat(locationY.getText()));
        } catch (Exception e) {
            showLog("输入坐标格式错误");
        }
    } // GEN-LAST:event_locationButtonActionPerformed

    /**
     * 显示日志
     *
     * @param log
     */
    public void showLog(String log) {
        logTextArea.append("\r\n");
        String time = TimeUtil.getDateTimeFormat(TimeUtil.YYYYMMDDHHMMSS);
        logTextArea.append(time + ":" + log);
    }
    // End of variables declaration//GEN-END:variables
}
